package DAL;
// Client.java
 
 // import java.net.Socket;
 import java.io.*;
import java.net.UnknownHostException;
import java.util.Properties;

import BL.User;
 
 public class Client {
	 
	 java.net.Socket socket;
	 Properties prop = new Properties();
	 String host = "eiabea.no-ip.org";
	 int port = 11111;
 	 //prop.load(new FileInputStream("client.properties"));
 	 
	 
	 public Client () {
		 
		 
		 try {
			this.prop.load(new FileInputStream("client.properties"));
		} catch (FileNotFoundException e) {
			System.out.println("---File: client.properties not found---");
			e.printStackTrace();
		} catch (IOException e) {
			System.out.println(e);
			e.printStackTrace();
		}
		 
		 //String host = prop.getProperty("host");
		 //int port = Integer.valueOf(prop.getProperty("port"));
		 this.host = prop.getProperty("host");
		 this.port = Integer.valueOf(prop.getProperty("port"));
		 
		 
		 try {
			this.socket = new java.net.Socket(host,port);
		} catch (UnknownHostException e) {
			System.out.println(e);
			e.printStackTrace();
		} catch (IOException e) {
			System.out.println(e);
			e.printStackTrace();
		}
	 }
     
     public void readUser(String nickname) throws IOException {
    	 

         try{

             String zuSendendeNachricht = "SELECT * FROM user WHERE nickname = '" + nickname + "'";
             schreibeNachricht(socket, zuSendendeNachricht);
             String empfangeneNachricht = leseNachricht(socket);
             System.out.println(empfangeneNachricht);
         }catch(IOException e){
         	System.out.println("Keine Verbindung!");
         }
     }
     
     public String startThread(String eingabe) throws FileNotFoundException, IOException{

         schreibeNachricht(socket, eingabe);
         String empfangeneNachricht = leseNachricht(socket);
         //System.out.println(empfangeneNachricht);
         return empfangeneNachricht;    	 
    	 
     }
     
     public void registerUser (User user) throws IOException {
    	 
    	 
    	 try {
    		 //java.net.Socket socket = new java.net.Socket(host,port);
    		 String message = "INSERT INTO user (nickname,password,vorname,nachname,mail) " +
    		 		"VALUES ('" + user.getNickName() + "','"
    		 		+ user.getPassword() + "','"
    		 		+ user.getFirstName() + "','"
    		 		+ user.getSecondName() + "','"
    		 		+ user.getMailAdress() + "')";
    		 schreibeNachricht(socket, message);
    		 String getMessage = leseNachricht(socket);
    		 System.out.println(user.getNickName() + " "+ getMessage);

    	 }catch(IOException e){
         	System.out.println("Keine Verbindung!");
         }
    	 
     }
     
     public void loginUser (User user) throws IOException {
    	 
    	 
    	 try {
    		 
    		 String message = "SELECT password FROM user WHERE nickname='" + user.getNickName() + "')";
    		 schreibeNachricht(socket, message);
    		 String getMessage = leseNachricht(socket);
    		 System.out.println(user.getNickName() + " "+ getMessage);

    	 }catch(IOException e){
         	System.out.println("Keine Verbindung!");
         }
    	 
     }
     

     private void schreibeNachricht(java.net.Socket socket, String nachricht) throws IOException {
         PrintWriter printWriter =
            new PrintWriter(
                new OutputStreamWriter(
                    socket.getOutputStream()));
        printWriter.print(nachricht);
        printWriter.flush();
    }
     
    private String leseNachricht(java.net.Socket socket) throws IOException {
        BufferedReader bufferedReader =
            new BufferedReader(
                new InputStreamReader(
                    socket.getInputStream()));
        char[] buffer = new char[200];
        int anzahlZeichen = bufferedReader.read(buffer, 0, 200); // blockiert bis Nachricht empfangen
        String nachricht = new String(buffer, 0, anzahlZeichen);
        return nachricht;
    }
 }